package com.myapp.Activity.AirLink;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.os.CountDownTimer;
import android.util.Log;
import android.widget.Button;
import android.widget.TextView;

import androidx.annotation.Nullable;

import com.gizwits.gizwifisdk.api.GizWifiDevice;
import com.gizwits.gizwifisdk.api.GizWifiSDK;
import com.gizwits.gizwifisdk.enumration.GizWifiConfigureMode;
import com.gizwits.gizwifisdk.enumration.GizWifiErrorCode;
import com.gizwits.gizwifisdk.enumration.GizWifiGAgentType;
import com.gizwits.gizwifisdk.listener.GizWifiSDKListener;
import com.myapp.Activity.MainActivity;
import com.myapp.R;
import com.myapp.RN.DeviceActivity;
import com.myapp.utils.PreferencesUtil;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

import static com.gizwits.gizwifisdk.enumration.GizWifiErrorCode.GIZ_SDK_DEVICE_CONFIG_IS_RUNNING;
import static com.myapp.Activity.AirLink.AirlinkChooseActivity.AIR_LINGKING;
import static com.myapp.Activity.AirLink.AirlinkChooseActivity.AP_NAME;
import static com.myapp.Activity.AirLink.AirlinkChooseActivity.WIFI_NAME;
import static com.myapp.Activity.AirLink.AirlinkChooseActivity.WIFI_PASSWORD;

public class AirlinkingActivity extends Activity {
    private static final String TAG = "AirlinkingActivity";
    @BindView(R.id.btn_airlink_cancer)
    Button btnAirlinkCancer;
    @BindView(R.id.tv_progress)
    TextView tvProgress;
    public static final String MAC = "mac";

    private List<GizWifiGAgentType> types = new ArrayList<GizWifiGAgentType>();
    private String ssid;
    private String key;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.dialog_activity_connecting);
        ButterKnife.bind(this);
        GizWifiSDK.sharedInstance().setListener(mListener);
        types.add(GizWifiGAgentType.GizGAgentESP);
        ssid = PreferencesUtil.getInstance(getApplicationContext()).getString(WIFI_NAME, null);
        key = PreferencesUtil.getInstance(getApplicationContext()).getString(WIFI_PASSWORD, null);

        Intent intent = getIntent();
        int status = intent.getIntExtra(AIR_LINGKING, 2);
        if (status == 0) {
            GizWifiSDK.sharedInstance().setDeviceOnboardingDeploy(ssid, key, GizWifiConfigureMode.GizWifiAirLink, null, 60, types, true);
        }


        if (status == 1) {
            String apName = PreferencesUtil.getInstance(getApplicationContext()).getString(AP_NAME, null);
            GizWifiSDK.sharedInstance().setDeviceOnboardingDeploy(ssid, key, GizWifiConfigureMode.GizWifiSoftAP, apName, 60, types, false);
            Log.i(TAG, "onCreate: " + "ssid:" + ssid + " key:" + key + " psw:" + apName);
        }
        timer.start();

    }


    @Override
    protected void onResume() {
        super.onResume();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        timer.cancel();
    }

    @Override
    public void onBackPressed() {

    }

    private CountDownTimer timer = new CountDownTimer(60000, 600) {
        int i = 0;

        @Override
        public void onTick(long millisUntilFinished) {
            i++;
//            String rate = String.format(Locale.getDefault(),"%d%%",i);
//            tvConnectRate.setText(rate);
            String number = String.valueOf(i);
            tvProgress.setText(number);
        }

        @Override
        public void onFinish() {
            i = 0;
            Log.i(TAG, "onFinish: ");
        }
    };

    GizWifiSDKListener mListener = new GizWifiSDKListener() {
        @Override
        public void didSetDeviceOnboarding(GizWifiErrorCode result, GizWifiDevice device) {
            super.didSetDeviceOnboarding(result, device);
            if (result == GizWifiErrorCode.GIZ_SDK_SUCCESS) {
                // 配置成功
                timer.cancel();
                PreferencesUtil.getInstance(getApplicationContext()).putString(MAC, device.getMacAddress());
                Log.i(TAG, "didSetDeviceOnboarding: "+device.getMacAddress());
                Intent success = new Intent(getApplicationContext(), DeviceActivity.class);
                startActivity(success);
                finish();

            } else if (result == GIZ_SDK_DEVICE_CONFIG_IS_RUNNING) {
                // 正在配置

            } else {
                // 配置失败
                timer.cancel();
                Intent intent = new Intent(getApplicationContext(), AirlinkFailActivity.class);
                startActivity(intent);
                finish();
            }
        }
    };

}
